package com.chelun.support.clmutiplyfragment;

import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created on 15/9/16.
 *
 * @author cxd
 */
public class ClMultiPtrDefaultHeader extends FrameLayout implements PtrUIHandler {

    private View viewHeader;
    private ObjectAnimator rotationAnimator;
    private ImageView img;

    public ClMultiPtrDefaultHeader(Context context) {
        super(context);
        init();
    }

    public ClMultiPtrDefaultHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClMultiPtrDefaultHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ClMultiPtrDefaultHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        viewHeader = LayoutInflater.from(getContext())
                .inflate(R.layout.clmulti_widget_list_pull_refresh_headview, this);
        img = (ImageView) viewHeader.findViewById(R.id.clMulti_xlistview_header_arrow);

        rotationAnimator = ObjectAnimator.ofFloat(img, "rotation", 1).setDuration(1000);
        rotationAnimator.setInterpolator(new LinearInterpolator());
        rotationAnimator.setRepeatCount(-1);
    }

    public void gone() {
        img.setVisibility(INVISIBLE);
    }

    @Override
    public void onUIReset(PtrFrameLayout ptrFrameLayout) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {

    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
//        MediaUtil.getInstance(getContext()).stop(); //下拉刷新的时候,要暂停语音播放
        //  TODO 在项目中ui刷新时暂停语音播放

        float currentRotation = img.getRotation();
        rotationAnimator.setFloatValues(currentRotation, 359f + currentRotation);
        rotationAnimator.start();
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
        rotationAnimator.cancel();
        img.setRotation(0f);
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout ptrFrameLayout, boolean b, byte b1, PtrIndicator ptrIndicator) {
        final int mOffsetToRefresh = ptrFrameLayout.getOffsetToRefresh();
        final int currentPos = ptrIndicator.getCurrentPosY();
        float percent = currentPos * 1.0f / mOffsetToRefresh;
        img.setRotation(percent * 360f * 1.0f);

    }
}
